//
//  HMPayViewController.m
//  HowToDo
//
//  Created by 李贺 on 2017/1/8.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMPayViewController.h"
#import "UILabel+Addition.h"
#import "HMPayTableViewCell.h"

@interface HMPayViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, assign) int totalNumber;
@property (nonatomic, strong) UILabel *totalMoneyLable;

@end

@implementation HMPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = @"账户余额";
    self.totalNumber = 0;
    [self makePayTableView];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

///余额界面
- (void)makePayTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 0.3)];
    headerView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
    tableView.tableHeaderView = headerView;
    
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 0.3)];
    footerView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
    tableView.tableFooterView = footerView;
    tableView.separatorInset = UIEdgeInsetsZero;
    
    //用class来注册cell
    [tableView registerClass:[HMPayTableViewCell class] forCellReuseIdentifier:@"HMPayTableViewCell_ID"];
    
    UILabel *totalMoneyLable = [UILabel hm_labelWithText:@"¥0" fontSize:60 color:[UIColor orangeColor]];
    
    [headerView addSubview:totalMoneyLable];
    [totalMoneyLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(headerView);
    }];
    [totalMoneyLable sizeToFit];
    self.totalMoneyLable = totalMoneyLable;
    
    UILabel *leftLable = [UILabel hm_labelWithText:@"充值" fontSize:13 color:[UIColor lightGrayColor]];
    [headerView addSubview:leftLable];
    [leftLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(16);
        make.bottom.equalTo(headerView.mas_bottom).offset(-8);
    }];
    [totalMoneyLable sizeToFit];
    
    UILabel *rightLable = [UILabel hm_labelWithText:@"此处充值仅限iOS系统使用" fontSize:13 color:[UIColor lightGrayColor]];
    [headerView addSubview:rightLable];
    [rightLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headerView.mas_right).offset(-16);
        make.bottom.equalTo(headerView.mas_bottom).offset(-8);
    }];
    [totalMoneyLable sizeToFit];
    
    UILabel *bottomLable = [UILabel hm_labelWithText:@"苹果和非iOS终端的充值不能相互通用,且虚拟产品必须通过苹果系统支付购买。" fontSize:13 color:[UIColor lightGrayColor]];
    [footerView addSubview:bottomLable];
    [bottomLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footerView.mas_right).offset(-16);
        make.left.equalTo(footerView.mas_left).offset(16);
        make.top.equalTo(footerView.mas_top).offset(8);
    }];
    bottomLable.numberOfLines = 0;
    [totalMoneyLable sizeToFit];
    
}

#pragma mark -tableView的代理-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HMPayTableViewCell * payCell = [tableView dequeueReusableCellWithIdentifier:@"HMPayTableViewCell_ID"];
    payCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString * payMoney = [NSString stringWithFormat:@"¥%.2f元",pow(4.0,indexPath.row+1)];
    payCell.textLabel.text = payMoney;
    payCell.textLabel.textColor = [UIColor orangeColor];
    payCell.payBtn.tag = indexPath.row;
    
    [payCell.payBtn addTarget:self action:@selector(payBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    return payCell;
}

///cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}

#pragma mark --点击按钮进行充值--
- (void)payBtnClick:(UIButton *)payBtn{
    self.totalNumber = self.totalNumber + pow(4.0,payBtn.tag+1);
    NSString *totalMoneyStr = [NSString stringWithFormat:@"¥%d",self.totalNumber];
    self.totalMoneyLable.text = totalMoneyStr;
    if(self.payDetailed){
        self.payDetailed(self.totalNumber);
    }
}

@end



